/**
 * @Description:  # TODO 获取验证码
 * @Author: 文渊
 * @Date: 2023-08-28 14:21:26
 */
function getCode() {
    //利用axios发送ajax请求
    const axios = ajax()
    axios({
        url: "/admin/checkcode"
    })
    .then(res => {
        const code = document.getElementById("code")
        code.innerHTML = res.msg
        //将验证码存储到storage当中
        localStorage.setItem("code", res.msg)
    })
}
getCode();


/**
 * @Description:  # TODO 登录操作
 * @Author: 文渊
 * @Date: 2023-08-28 14:21:19
 */
window.onload = function () {
    //校验账号和密码是否合格
    checkInfologin();
    //实现按键登录功能
    const login = document.getElementById("login")
    login.onclick = loginHandler

    //按回车键实现登录
    const inplogin = document.getElementById("inpcode")
    inplogin.onkeyup = function (e) {
        if (e.keyCode == 13) {
            loginHandler()
        }
    }
}


// 当页面一打开的时候,从服务器获取验证码
let usernameflag = false;
let passwordflag = false;
function checkInfologin() {
    /* 
        账号的文本框
    */
    const username = document.getElementById("username")
    const usernamespan = document.getElementById("usernamespan")
    username.addEventListener("change", (e) => {
        //显示提示框
        usernamespan.style.display = "block"
        const reg = /^[0-9a-z]{5,15}$/i
        if (reg.test(username.value)) {
            usernamespan.innerHTML = "账号校验合格";
            usernamespan.style.color = "green"
            usernameflag = true;
        } else {
            usernamespan.innerHTML = "账号校验不合格";
            usernamespan.style.color = "red"
            usernameflag = false;
        }
    })
    /* 
        密码的文本框
    */
    const password = document.getElementById("password")
    const passwordspan = document.getElementById("passwordspan")
    password.addEventListener("change", (e) => {
        //显示提示框
        passwordspan.style.display = "block"
        const reg = /^[0-9a-z]{5,15}$/i
        if (reg.test(password.value)) {
            passwordspan.innerHTML = "密码校验合格";
            passwordspan.style.color = "green"
            passwordflag = true;
        } else {
            passwordspan.innerHTML = "密码校验不合格";
            passwordspan.style.color = "red"
            passwordflag = false;
        }
    })
}

function loginHandler() {
    //获取账号密码
    const username = document.getElementById("username").value;
    const password = document.getElementById("password").value;
    //校验正确
    if (usernameflag && passwordflag) {
        //获取你输入的验证码
        const inpcode = document.getElementById("inpcode").value
        //获取你刚刚存储storage里面的验证码
        const stocode = localStorage.getItem("code")
        //可以登录
        if (inpcode == stocode) {
            //发送ajax,进行登录操作
            const axios = ajax()
            axios({
                url: "/admin/login",
                method: "post",
                data: {
                    username, password
                }
            })
            .then(res => {
                // 将用户信息和token存储起来
                localStorage.setItem("token", res.token)
                localStorage.setItem("userInfo", JSON.stringify(res.msg))
                //跳转到主页
                window.location.href = "./home.html"
            })
        } else {
            alert("验证码错误")
        }
    } else {
        alert("账号或密码校验不合格")
    }
}